/**
 * 
 */
package com.cvlep.appbase.util;

import net.sf.json.JSONObject;

import com.cvlep.appbase.Constants;
import com.cvlep.appbase.message.Message;

/**
 * @author Otupia
 *
 */
public final class MessageUtils
{
	/**
	 * Compress message to a String 
	 * The format of String depended on the Constatns.MESSAGE_COMPRESS_FORMATE
	 * @param message
	 * @return
	 */
	public static String compressMessage(Message message)
	{
		if(Constants.MESSAGE_COMPRESS_FORMATE.equalsIgnoreCase("JSON"))
		{
			return compressMessageToJSON(message);
		}
		
		return null;
	}
	
	/**
	 * Express message from a String 
	 * The format of String depended on the Constatns.MESSAGE_COMPRESS_FORMATE
	 * @param messageAsJSON
	 * @return
	 */
	public static Message expressMessage(String messageAsJSON)
	{
		if(Constants.MESSAGE_COMPRESS_FORMATE.equalsIgnoreCase("JSON"))
		{
			return expressMessageFromJSON(messageAsJSON);
		}
		
		return null;
	}
	
	/**
	 * To compress a message as a json style string.
	 * @param message
	 * @return
	 */
	private static String compressMessageToJSON(Message message)
	{
		JSONObject tmpJo = JSONObject.fromObject(message);
		
		return tmpJo.toString();
	}
	
	/**
	 * To express a message from json
	 * @param messageAsJSON
	 * @return
	 */
	private static Message expressMessageFromJSON(String messageAsJSON)
	{
		JSONObject tmpJo = JSONObject.fromObject(messageAsJSON);
		
		return (Message)JSONObject.toBean(tmpJo, Message.class);
	}
	
	
	
	
}
